package com.aaa.statistics.service.impl;

import com.aaa.statistics.dao.OrderChargeItemDao;
import com.aaa.statistics.service.RevenueService;
import com.aaa.statistics.vo.RevenueVo;
import com.aaa.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

/**
 * @author Lei
 * @create 2023-03-31 19:47
 */
@Service
public class RevenueServiceImpl implements RevenueService {

    @Autowired
    private OrderChargeItemDao orderChargeItemDao;

    @Override
    public Result<Map<String,Object>> queryAllRevenueData(RevenueVo revenueVo) {

        String beginTime = "";
        String endTime = "";
        if (Objects.nonNull(revenueVo.getDateRange())){
            //如果查询条件不为空，需要查询范围时间的收支情况
            String[] dateRange = revenueVo.getDateRange();
            beginTime = dateRange[0];
            endTime = dateRange[1];
        }else {
            //查询条件为空就是查询当天的收支情况
            beginTime = LocalDate.now().minusDays(1).toString();
            endTime = LocalDate.now().plusDays(1).toString();
        }
        //成功支付的支付宝数据
        Double alipayIncome = orderChargeItemDao.selectChargeByCon(beginTime,endTime,"1");
        //成功支付的现金数据
        Double cashIncome = orderChargeItemDao.selectChargeByCon(beginTime,endTime,"0");
        //成功退费的支付宝数据
        Double alipayRefund = orderChargeItemDao.selectBackFeeByCon(beginTime,endTime,"1");
        //成功退费的现金数据
        Double cashRefund = orderChargeItemDao.selectBackFeeByCon(beginTime,endTime,"0");

        //当从数据库查询当天的总数为空时，将总数设置为0.0
        if (!Objects.nonNull(alipayIncome)){
            alipayIncome = 0.0;
        }
        if (!Objects.nonNull(cashIncome)){
            cashIncome = 0.0;
        }
        if (!Objects.nonNull(alipayRefund)){
            alipayRefund = 0.0;
        }
        if (!Objects.nonNull(cashRefund)){
            cashRefund = 0.0;
        }

        if (Objects.nonNull(alipayIncome) && Objects.nonNull(cashIncome)&& Objects.nonNull(alipayRefund) && Objects.nonNull(cashRefund)){
            //总收入
            Double toll = alipayIncome+cashIncome;
            //总退费
            Double refundTotal = alipayRefund+cashRefund;
            //合计收入
            Double totalRevenue = (alipayIncome+cashIncome)-(alipayRefund+cashRefund);
            //收支情况：{总收入，总退费}
            Map<String,Object> overview = new HashMap<>();
            // 收入渠道: 现金支付￥0 支付宝支付￥0 现金退费￥0 支付宝退费￥0
            Map<String,Object> channel = new HashMap<>();
            overview.put("toll",toll);  //总收入
            overview.put("refund",refundTotal);//总退费
            channel.put("cashIncome",cashIncome); //现金收入
            channel.put("alipayIncome",alipayIncome);//支付宝收入
            channel.put("cashRefund",cashRefund);//现金退费
            channel.put("alipayRefund",alipayRefund);//支付宝退费

            Map<String,Object> revenueObj = new HashMap<>();
            revenueObj.put("channel",channel);
            revenueObj.put("overview",overview);
            revenueObj.put("totalRevenue",totalRevenue);

            //封装收支情况
            Map<String,Object> revenueOverview = new HashMap<>();
            Map<String,Object> revunue1 = new HashMap<>();
            revunue1.put("name","收费金额");
            revunue1.put("value",alipayIncome+cashIncome);
            Map<String,Object> revunue2 = new HashMap<>();
            revunue2.put("name","退费金额");
            revunue2.put("value",alipayRefund+cashRefund);
            List<Map<String,Object>> revenueData = new ArrayList<>();
            revenueData.add(revunue1);
            revenueData.add(revunue2);
            revenueOverview.put("data",revenueData);
            revenueOverview.put("title","收支概况");

            //封装收入渠道
            Map<String,Object> incomeChanel = new HashMap<>();
            Map<String,Object> income1 = new HashMap<>();
            income1.put("name","现金收入");
            income1.put("value",cashIncome);
            Map<String,Object> income2 = new HashMap<>();
            income2.put("name","微信收入");
            income2.put("value",alipayIncome);
            List<Map<String,Object>> incomeData = new ArrayList<>();
            incomeData.add(income1);
            incomeData.add(income2);
            incomeChanel.put("data",incomeData);
            incomeChanel.put("title","收入渠道");

            //封装退款
            Map<String,Object> refund = new HashMap<>();
            Map<String,Object> refund1 = new HashMap<>();
            refund1.put("name","现金退费");
            refund1.put("value",cashRefund);
            Map<String,Object> refund2 = new HashMap<>();
            refund2.put("name","微信退费");
            refund2.put("value",alipayRefund);
            List<Map<String,Object>> refundData = new ArrayList<>();
            refundData.add(refund1);
            refundData.add(refund2);
            refund.put("title","退款");
            refund.put("data", refundData);

            //封装所有的数据
            Map<String,Object> totalResult = new HashMap<>();
            totalResult.put("revenueObj",revenueObj);
            totalResult.put("revenueOverview",revenueOverview);
            totalResult.put("incomeChanel",incomeChanel);
            totalResult.put("refund",refund);
            return new Result<>(200,"成功",totalResult);
        }
        return new Result<>(200,"没值",null);
    }


}
